package com.neusoft.lohas.common.utils;

import static com.neusoft.lohas.common.utils.StaticValue.ERROR;
import static com.neusoft.lohas.common.utils.StaticValue.SUCCESS;

import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

public abstract class InputCheck {
	private static ConcurrentHashMap<String, Pattern> cachedPatternMap = new ConcurrentHashMap<String, Pattern>(
			15);
	// private static Pattern pattern;
	/*
	 * 包括-1和正数的验证
	 */
	public static String INCLUDE_UNLOGIN = "^-1|[1-9][0-9]*";
	public static String INTERGER = "[-,+]{0,1}[0-9]*";
	/**
	 * id的合法性
	 */
	public static String POSITIVE_INTEGER = "[1-9][0-9]{0,}";
	/**
	 * id的合法性
	 */
	public static String _ID_INTEGER = "-1|[0-9]{0,}";
	/**
	 * 包括0的数字
	 */
	public static String POSITIVE_INTEGER_ZERO = "[0-9]+";

	public static String IDS = "(?:(?:[0-9]+),+)+";
	/**
	 * 邮件的验证
	 */
	public static String EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";

	/**
	 * 手机号码匹配
	 */
	public static String PHONE = "^1[2,3,4,5,7,8]{1}\\d{9}";

	public static String PHONE_LIST = "(?:(?:1[2,3,4,5,7,8]{1}\\d{9}+),+)+";
	/**
	 * 电话号码的匹配
	 */
	public static String _Tel = "(?:\\d{4}[-]{0,2}\\d{8})|(?:\\d{4}[-]{0,2}\\d{7})|(?:\\d{3}[-]{0,2}\\d{8})";

	/**
	 * 邮政编码
	 */
	public static String _ZIPCODE = "\\d{6}";

	/**
	 * 日期
	 */
	public static String DATE_YYYYMMDD = "20[0-9]{2}(0[1-9]|1[0,1,2])(?:0[1-9]|[1,2][0-9]|3[0,1])$";
	/**
	 * 日期
	 */
	public static String BIRTH_YYYYMMDD = "[1,2][0-9]{3}(0[1-9]|1[0,1,2])(?:0[1-9]|[1,2][0-9]|3[0,1])$";

	public static String _TIME_YYYYMMDDHHMMSS = "20[0-9]{2}(0[1-9]|1[0,1,2])(?:0[1-9]|[1,2][0-9]|3[0,1])[0-9]{6}$";
	/**
	 * 不包含特殊字符
	 */
	public static String _GROUP_NAME_EXCLUDE_CHAR = "[^`~!@#$^&*()+=|{}':;'\",\\[\\].<>/?~！@#￥……&*（）——+|{}【】‘；：”“’。，、？]{0,}";

	public static String _ADDRESS_NAME_EXCLUDE_CHAR = "[^`~!@#$%^&*()+=|{}':;'\\[\\]<>/?~！@#￥%……&*（）+|{}【】‘；：”“’。、？]{1,}";

	public static String _FILE_NAME = "[^`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]{1,}\\.\\w+";
	public static String _ENUM = "[0-9]{0,2}";
	public static String _NAME = _GROUP_NAME_EXCLUDE_CHAR;
	public static String _NUMBER_CHAR = "^[a-zA-Z0-9_]+$";

	/**
	 * 初始化正则表达式
	 * @param regex
	 */
	private static Pattern init(String regex)
	{
		if (cachedPatternMap.contains(regex))
			return cachedPatternMap.get(regex);
		Pattern pattern = Pattern.compile(regex);
		cachedPatternMap.put(regex, pattern);
		return pattern;
	}

	/**
	 * 对输入的Id做检测，规则使大于0的数
	 * @param regex 输入的正则表达式
	 * @param inputValue 验证值
	 * @param fieldName 这个值的名称
	 * @return
	 */
	public static int IdCheck(String regex, String inputValue, String fieldName)
	{
		if (regex == null)
			return SUCCESS;
		if (inputValue == null)
			return ERROR;
		Pattern pattern = init(regex);
		inputValue = inputValue.trim();
		Matcher macher = pattern.matcher(inputValue);
		boolean matche = macher.matches();
		if (matche)
		{
			return SUCCESS;
		}
		return ERROR;
	}

	/**
	 * 对输入的Id做检测，规则使大于0的数
	 * @param regex 输入的正则表达式
	 * @param inputValue 验证值
	 * @param fieldName 这个值的名称
	 * @return
	 */
	public static int IdCheck(String regex, String inputValue)
	{
		if (regex == null)
			return SUCCESS;
		if (inputValue == null)
			return ERROR;
		Pattern pattern = init(regex);
		inputValue = inputValue.trim();
		Matcher macher = pattern.matcher(inputValue);
		boolean matche = macher.matches();
		if (matche)
		{
			return SUCCESS;
		}
		return ERROR;
	}

	/**
	 * 对字符安正则表达式匹配
	 * @param regex
	 * @param inputValue
	 * @param fieldName
	 * @return
	 */
	public static int stringCheck(String regex, String inputValue)
	{
		if (regex == null)
			return SUCCESS;
		if (inputValue == null)
			return ERROR;
		Pattern pattern = init(regex);
		inputValue = inputValue.trim();
		Matcher macher = pattern.matcher(inputValue);
		boolean matche = macher.matches();
		if (matche)
		{
			return SUCCESS;
		}
		return ERROR;
	}

	public static int couldNullStringCheck(String regex, String inputValue)
	{
		if (regex == null)
			return SUCCESS;
		if (inputValue == null)
			return SUCCESS;
		Pattern pattern = init(regex);
		inputValue = inputValue.trim();
		Matcher macher = pattern.matcher(inputValue);
		boolean matche = macher.matches();
		if (matche)
		{
			return SUCCESS;
		}
		return ERROR;
	}

	public static int couldNullSpaceStringCheck(String regex, String inputValue)
	{
		if (regex == null)
			return SUCCESS;
		if (inputValue == null || inputValue.equals(""))
			return SUCCESS;
		Pattern pattern = init(regex);
		inputValue = inputValue.trim();
		Matcher macher = pattern.matcher(inputValue);
		boolean matche = macher.matches();
		if (matche)
		{
			return SUCCESS;
		}
		return ERROR;
	}

	public static int appIdCheck(String apppId)
	{
		if (apppId == null)
			return SUCCESS;
		else if (apppId.startsWith("002002"))
			return SUCCESS;
		else {
			return ERROR;
		}
	}

	/**
	 * 检查是否是整数
	 * @param inputInt
	 * @return
	 */
	public static boolean isInt(String inputInt) {
		Pattern pattern = init("^[-,+]?[0-9]+");
		Matcher matcher = pattern.matcher(inputInt);
		return matcher.matches();

	}

	/**
	 * 检查Id是否在ids列表中
	 * @param ids
	 * @param userId
	 * @return
	 */
	public static boolean includingSelf(String ids, String userId)
	{
		if (ids == null || userId == null)
			return true;
		String id = "," + userId + ",";
		String ides = "," + ids + ",";

		if (!isIds(ids))
			return true;
		Pattern pattern = init(id);
		Matcher matcher = pattern.matcher(ides);
		// System.out.println(matcher.find());
		boolean match = matcher.find();

		return match;
	}

	/**
	 * 检查Ids是否是合法的逗号隔开的Id列表
	 * @param ids
	 * @return
	 */
	public static boolean isIds(String ids)
	{
		if (ids == null)
			return false;
		if (ids.equals(""))
			return true;
		Pattern pattern = init("(?:(?:[0-9]+),+)+");
		Matcher matcher = pattern.matcher(ids + ",");
		return matcher.matches();
	}

	public static boolean isIdsOrNull(String ids)
	{
		if (ids == null)
			return true;
		Pattern pattern = init("(?:(?:[0-9]+),+)+");
		Matcher matcher = pattern.matcher(ids + ",");
		return matcher.matches();
	}

	/**
	 * 返回错误的JSON错误信息
	 * @param message
	 * @return
	 */
	public static JSONObject validateJson(String message)
	{
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("status", "1");
		jsonObject.put("message", message);
		return jsonObject;
	}

	public static void main(String[] arg)
	{
		// init(DATE_YYYYMMDD);
		// Matcher matcher = pattern.matcher("20130130");
		// System.out.println(includingSelf("1,2,3,4,5", "4"));"\\.\\w+"
		// init(_FILE_NAME);
		// Matcher matcher =pattern.matcher("");
		// System.out.println(matcher.matches());
		// String aString = "12323_90";
		// System.out.println(aString.replace("_", "0"));
		// System.out.println(aString);
		//
		// System.out.println(matcher.find());]

		System.out.println(stringCheck(PHONE_LIST, "13764183140,13745611234"));
	}
}
